package ui.activity.home;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.Spanned;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jakewharton.rxbinding.view.RxView;

import net.ck.hzq.R;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import Bean.ProductBean;
import base.BaseAct;
import butterknife.BindView;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import cn.finalteam.loadingviewfinal.ScrollViewFinal;
import coom.Constants;
import event.NetErrorEvent;
import ui.activity.home.biz.ProductBiz;
import ui.activity.home.component.DaggerProductComponent;
import ui.activity.home.contract.ProductContract;
import ui.activity.home.module.ProductModule;
import ui.activity.home.presenter.ProductPresenter;
import ui.activity.order.ConfirmOrderAct;
import util.HtmlTagHandler;
import util.StringUtils;

/**
 * 商品详情
 */
public class ProductAct extends BaseAct implements ProductContract.View {


    @Inject
    ProductPresenter presenter;
    @Inject
    ProductBiz biz;

    @BindView(R.id.product_image)
    SimpleDraweeView productImage;
    @BindView(R.id.product_text1)
    TextView productText1;
    @BindView(R.id.product_text2)
    TextView productText2;
    @BindView(R.id.product_text3)
    TextView productText3;
    @BindView(R.id.product_text4)
    TextView productText4;
    @BindView(R.id.product_text5)
    TextView productText5;
    @BindView(R.id.product_text6)
    TextView productText6;
    @BindView(R.id.product_text7)
    TextView productText7;
    @BindView(R.id.product_text8)
    TextView productText8;
    @BindView(R.id.product_text9)
    TextView productText9;
    @BindView(R.id.sv)
    ScrollViewFinal sv;
    @BindView(R.id.ptr_layout)
    PtrClassicFrameLayout ptr;
    @BindView(R.id.but_product_myorder)
    Button order;
    @BindView(R.id.but_product_depositadeposit)
    Button submit;


    private String id;
    ProductBean.DataBean.ItemBean itemBean;

    @Override
    protected void setRoot() {
//        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_product);

    }

    @Override
    protected void setup() {
        DaggerProductComponent.builder().productModule(new ProductModule(this)).build().inject(this);
        setToolBar(getResources().getString(R.string.lang_product), "");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        Constants.activitys.clear();
        Constants.activitys.add(this);
        id = StringUtils.isEmpty(getIntent().getStringExtra(Constants.KEYSTRING)) ? "0" : getIntent().getStringExtra(Constants.KEYSTRING);
        sv.setOnLoadMoreListener(() -> {
        });
        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.getPosModel(true, id);
            }
        });
        ptr.autoRefresh(true);
        RxView.clicks(order)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toOrder());
        RxView.clicks(submit)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toSubmitOrder());
    }


    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }


    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }


    @Override
    public void loadMoreComplete() {
        sv.onLoadMoreComplete();
    }


    @Override
    public void hasLoadMore(boolean b) {
        sv.setHasLoadMore(false);
    }

    @Override
    public void refreshComplete() {
        ptr.onRefreshComplete();
    }

    @Override
    public void upDateUI(ProductBean.DataBean bean) {
        itemBean = bean.getItem();
        productImage.setImageURI(Uri.parse(bean.getItem().getImgPath()));
        productText1.setText(bean.getItem().getName());
        productText2.setText(bean.getItem().getSellName());
        String info = "激活<myfont color=#FF4040 size='30px' >返现" + bean.getItem().getReturnAmount() + "元/台</myfont>";
        Spanned str = Html.fromHtml(info, null, new HtmlTagHandler("myfont"));
        productText3.setText(str);
        productText4.setText(bean.getItem().getActDescription());
        productText5.setText(bean.getItem().getActTimeDesc());
        productText6.setText("¥ " + bean.getItem().getPrice());
        productText7.setText(bean.getItem().getName());
        productText8.setText(bean.getItem().getNumber() + "台");
        productText9.setText("¥ " + bean.getItem().getPrice());
    }

    @Override
    public void submitOrder() {
        if (itemBean == null) {
            showMsg("数据错误，请刷新页面");
            return;
        }
        Intent intent = new Intent(ProductAct.this, ConfirmOrderAct.class);
        intent.putExtra("id", itemBean.getId());
        intent.putExtra("pic", itemBean.getImgPath());
        intent.putExtra("sellName", itemBean.getSellName());
        intent.putExtra("price", itemBean.getPrice());
        intent.putExtra("buyNumber", "1");
        intent.putExtra("totalPrice", itemBean.getPrice());
        startActivity(intent);
    }

}

